<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Category;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CategoryController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Category(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('is_recommend')->display(function ($status) {
                if ($status == 0) {
                    return "<span style='color:green'>不推荐</span>";
                }
                if ($status == 1) {
                    return "<span style='color:red'>推荐</span>";
                }
            });
            $grid->column('icon')->image('', 100, 100);
            $grid->column('sort')->sortable();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->export();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
            $grid->disableViewButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Category(), function (Show $show) {
            $show->field('id');
            $show->field('id');
            $show->field('title');
            $show->field('icon')->image();
            $show->field('sort');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Category(), function (Form $form) {
            $form->display('id');
            $form->text('title')->required();
            $form->select('is_recommend')->options(function (){
                return [0=>'否', 1=>'是'];
            });
            $form->image('icon')->autoUpload()->uniqueName()->required();
            $form->number('sort')->default(100000);

            $form->display('created_at');
            $form->display('updated_at');

            $form->disableViewButton();
        });
    }
}
